<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
</head>

<body>
    <div id="app">
        <button v-debounce="handler1">点击</button>
        <button v-debounce="handler2">点击</button>
    </div>
</body>
<script>
    Vue.directive('debounce', {
        // Vue2特有生命周期
        // bind (el, binding) {
        // },
        // Vue2特有生命周期
        inserted (el, binding) {
            let timer = null
            // 获取节点，可以写dom操作
            el.addEventListener('click', () => {
                if (timer) clearInterval(timer)
                timer = setTimeout(() => {
                    console.log('防抖', binding.value);
                    binding.value()
                }, 500)
            })
        },
    })
    var app = new Vue({
        el: '#app',
        methods: {
            handler1 () { console.log('btn1'); },
            handler2 () { console.log('btn2'); }
        }
    })
</script>

</html>